﻿// ***************************************************************************
// YAMM
// Copyright (C) 2008 Christian Schmiljun, Frank Weißenborn 
// http://www.codeplex.com/YAMM 
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ***************************************************************************


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace YAMM
{
    using PlugInManagement;
    using PlugInManagement.Menu;
    using MenuBar = PlugInManagement.Menu.MenuBar;
    using Window = PlugInManagement.Windows;

    public partial class MainForm : Form
    {
        ToolStripMenuManager menuManager;
        ServiceProvider serviceProvider = new ServiceProvider();

        Window.WindowManager windowManager;
        WeifenLuo.WinFormsUI.Docking.DockPanel dockpanel;

        public MainForm()
        {
            InitializeComponent();

            // load App Icon
            Icon = (Icon)global::YAMM.Properties.Resources.ResourceManager.GetObject("ApplicationIcon");

            // PluginManagement / Menumanagement
            menuManager = new ToolStripMenuManager(this, toolStripContainer1);
            // eigene Programmmenus registrieren
            MenuBar.ToolStripMenu mainmenu = (MenuBar.ToolStripMenu)(menuManager.Items.AddMenuBar(PlugIn.StandardIdentifiers.MainMenu, "Mainmenu"));
            this.MainMenuStrip = mainmenu;

            PlugIn.IToolStripSplitButton file = mainmenu.Items.AddSubMenuSplitButton(Guid.NewGuid(), "&File", null);
            PlugIn.IToolStripButton exit = file.Items.AddButton(Guid.NewGuid(), "E&xit", new EventHandler(ApplicationExitHandler));

            PlugIn.IToolStripSplitButton extras = mainmenu.Items.AddSubMenuSplitButton(PlugIn.StandardIdentifiers.MainMenuExtra, "&Einstellungen", null);
            PlugIn.IToolStripButton pluginManagerGUI = extras.Items.AddButton(Guid.NewGuid(), "Plugins", new EventHandler(ShowPluginManagerHandler), Keys.Alt | Keys.P);

            PlugIn.IToolStripSplitButton help = mainmenu.Items.AddSubMenuSplitButton(Guid.NewGuid(), "&Hilfe", null);

            serviceProvider.AddService(menuManager);

            // PluginManagement / Windowmanagement
            dockpanel = new WeifenLuo.WinFormsUI.Docking.DockPanel();
            dockpanel.Dock = DockStyle.Fill;
            dockpanel.DocumentStyle = WeifenLuo.WinFormsUI.Docking.DocumentStyle.DockingWindow;
            toolStripContainer1.ContentPanel.Controls.Add(dockpanel);

            windowManager = new YAMM.PlugInManagement.Windows.WindowManager(dockpanel);
            serviceProvider.AddService(windowManager);

            // StartSeite
            ContentForms.StartWindow contentStartWindow = new ContentForms.StartWindow();
            PlugIn.IWindow startWindow = windowManager.Items.Add(contentStartWindow.Guid, "Startseite", contentStartWindow);
            startWindow.Visible = true;
            // StartSeite ins Menu eintragen
            help.Items.AddButton(Guid.NewGuid(), "Startfenster", delegate(object sender, EventArgs e) { startWindow.Visible = true; });

            // About Dialog
            help.Items.AddSeparator(Guid.NewGuid());
            PlugIn.IToolStripButton info = help.Items.AddButton(Guid.NewGuid(), "Info über YAMM", delegate(object sender, EventArgs e) { new About().ShowDialog(); });


            // TEST            
            //mainmenu.Items.AddButton(Guid.NewGuid(), "Form1", delegate(object sender, EventArgs e) { new Form1().Show(); });
            //PlugIn.IToolStripMenu toolbar = menuManager.Items.AddToolBar(Guid.NewGuid());
            //PlugIn.IToolStripSplitButton btn =
            //    toolbar.Items.AddSubMenuSplitButton(Guid.NewGuid(), "Hallo", new EventHandler(ApplicationExitHandler), ((Icon)global::YAMM.Properties.Resources.ResourceManager.GetObject("ApplicationIcon")).ToBitmap());
            //btn.Items.AddButton(Guid.NewGuid(), "tztztrtz", delegate(object sender, EventArgs e) { btn.PerformClick(); });
            //toolbar.Items.AddSeparator(Guid.NewGuid());
            //toolbar.Items.AddButton(Guid.NewGuid(), "tzrzrt", null).Enabled = false;            

            PluginManager.Init(serviceProvider);
        }

        void ApplicationExitHandler(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void ShowPluginManagerHandler(object sender, EventArgs e)
        {
            PluginManagerForm plugins = new PluginManagerForm();
            plugins.ShowDialog(this);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //TODO: Fenster speichern

            //Execute Uninstall methods
            foreach (Job job in Jobs.jobs)
            {
                switch (job.id)
                {
                    case JobType.Add:
                        break;
                    case JobType.Remove:
                        foreach (PlugIn.IPlugin item in PluginManager.PluginList)
                        {
                            if (String.Compare(item.Guid.ToString(),job.guid) == 0)
                            {
                                item.Uninstall();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
